Every tool change on our machine has a final step where it touches off the tool on a fixed height tool setter. What I am trying to do is write the tool length back to KMotion-CNC, but I am getting some unexpected behavior: "old" tool heights are getting rolled out of the table. For example, if I change from tool 1 to tool 3, then I can see the tool 3 entry being written (using the "edit" button on the tool panel) but tool 1's measurement disappears. Not only that, the entry for tool ID 3 gets written to seemingly random entries in the tool table, sometimes leaving duplicate entries.
I'm probably just doing something wrong. Here is how I have it coded so far:
At the end of running the kflop C program for M6, the program obtains the Z position where the touch-off occurs. The code then runs the following routine to update the tool table:
// Main routine to update tool length offset to the CNC program. Since we touch off
// tools to a fixed THS, we get a relative tool height by reading Z at the touch-off point.
// We set the current TLO from Z plus an arbitrary offset to make the TLO roughly correspond
// to the actual length projecting from the spindle nose, and taking account of whether the
// CNC is currently in inches or mm.
// Note that the CNC program should be using G43 H<n> to see any effect.
// z parameter is the Z axis height at touch-off point
// Returns 0 if OK, 1 if could not query host.
int update_tlo(double z)
{
int units, TWORD, HWORD, DWORD;
double tlo;
if (VAR_CURRENT_TOOL <= 0)
return 0; // Ignore if no mounted tool
if (GetMiscSettings(&units, &TWORD, &HWORD, &DWORD)) // Just for units
return 1;
tlo = STEPS_TO_MM(z + TC_THS_TLO);
if (units != CANON_UNITS_MM)
tlo /= 25.4;
// Change Currently Selected Tool Length
return SetToolLength(VAR_CURRENT_TOOL, tlo);
}
The extern functions (SetToolLength) are stolen from your sample code. VAR_CURRENT_TOOL is the T number from the M6. I'm assuming a 1:1 map between tools and tool slots, indices etc.